﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OracleClient;

namespace Efficient.DataAccess.DbProvider
{
    [DbProvider(DbProviderType.OracleClient)]
    internal class OracleClientDbService : DbService, IDbService
    {
        private static readonly string computeRecordCountTemplate;
        private static readonly string pagingQueryTemplate;
        private static readonly string firstResultQueryTemplate;
        
        private const string SELECT = "SELECT ";

        static OracleClientDbService()
        {
            computeRecordCountTemplate = string.Concat("select count(*) from(", QUERY_TAG, ") __query");

            pagingQueryTemplate = string.Concat("select * from (select rownum as \"$$ROW\",a.* from(", QUERY_TAG ,") a where \"$$ROW\"<=", END_PLACE_HOLDER, ") __query where \"$$ROW\" > " , START_PLACE_HOLDER);  

            firstResultQueryTemplate = string.Concat("select * from (select rownum as \"$$ROW\",a.* from(", QUERY_TAG ,") a ) __query where \"$$ROW\" > " , START_PLACE_HOLDER);  
        }

        public OracleClientDbService(string connectionString) : base(connectionString) { }

        protected override string ComputeRecordCountTemplate
        {
            get { return computeRecordCountTemplate; }
        }

        public override IDbCommand CreateCommand()
        {
            return new OracleCommand();
        }

        public override IDbDataParameter CreateParameter()
        {
            return new OracleParameter();
        }

        public override IDbDataAdapter CreateDataAdapter()
        {
            return new OracleDataAdapter();
        }

        public override IDbConnection CreateConnection(string connectionString)
        {
            return new OracleConnection(connectionString);
        }

        #region IDbService Members

        public bool PagingNeedOrder
        {
            get { return false; }
        }

        public char CloseQuote
        {
            get { return '"'; }
        }

        public char OpenQuote
        {
            get { return '"'; }
        }

        public bool SupportLimit
        {
            get { return true; }
        }

        public bool SupportLimitOffset
        {
            get { return true; }
        }

        public string ParamNamedPrefix
        {
            get { return ":"; }
        }

        public string MaxResultQuery(string rawQuery, uint maxRow)
        {
            throw new NotImplementedException();
        }

        public string BuildPagingResultQueryNeedOrder(string rawQuery, string orderString, string startParam, string endParam)
        {
            throw new NotImplementedException();
        }

        public string BuildFirstResultQueryNeedOrder(string rawQuery, string orderString, string startParam)
        {
            throw new NotImplementedException();
        }

        public string BuildPagingResultQuery(string rawQuery, string startParam, string endParam)
        {
            throw new NotImplementedException();
        }

        public string BuildFirstResultQuery(string rawQuery, string startParam)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
